package com.java.design;

/**
 * 装饰器模式用于给现有的类添加一些额外的属性，它不同于是继承，继承是直接覆盖基类的方法，而装饰圈是要使用原类的方法
 * 
 * @author Administrator
 *
 */
//定义一个接口
interface Decoreatorable {
	public void method();
}

//某个类实现该接口
class DecoreatorSource implements Decoreatorable {
	public void method() {
		System.out.println("DecoreatorSource:method");
	}
}

//装饰器类实现接口并且使用某个实现了该接口的类
class Decoreator implements Decoreatorable {
	private DecoreatorSource decoreatorSource = null;

	public Decoreator(DecoreatorSource decoreatorSource) {
		this.decoreatorSource = decoreatorSource;
	}

	public void method() {
		System.out.println("Decoreator:before");
		decoreatorSource.method();
		System.out.println("Decoreator:after");
	}
}

//测试类
public class DecoratorMode {
	public static void main(String[] args) {
		DecoreatorSource decoreatorSource = new DecoreatorSource();
		Decoreatorable decoreatorable = new Decoreator(decoreatorSource);
		decoreatorable.method();
	}
}
